package com.wl.rjwmc.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wl.rjwmc.DoMain.Employee;
import com.wl.rjwmc.Service.EmployeeService;
import com.wl.rjwmc.Utools.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /***
     * 登录
     * @param request
     * @param employee
     * @return
     */
//HttpServletRequest 获取网页中信息 这里是获得账号密码
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //获取密码
        String password = employee.getPassword();
        // 通过md5给密码加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        // 查询网页获得的用户名和数据库的用户名是否一致
        lqw.eq(Employee::getUsername,employee.getUsername());
        //用于获取实体类数据
        Employee employeeOne = employeeService.getOne(lqw);
        if (employeeOne==null){
            return R.error("登录失败");
        }
        if(!employeeOne.getPassword().equals(password)){
            return R.error("密码错误");
        }
        if(employeeOne.getStatus()==0){
            return R.error("该用户以禁用");
        }
//        把ID添加到Session里
        request.getSession().setAttribute("employee",employeeOne.getId());
        return R.success(employeeOne);
    }

    /***
     * 登出
     * @param request
     * @return
     */
    @PostMapping("logout")
    public R<String> logout(HttpServletRequest request){
//        把Session中的id清除
        request.removeAttribute("employee");
        return R.success("退出成功");
    }

    /***
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<Employee>> list(int page,int pageSize, String name){
//        分页构造器
        Page<Employee> employeePage = new Page<>(page,pageSize);
//        查询构造器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
//        查询条件 名字非空
        lqw.like(Strings.isNotEmpty(name),Employee::getName,name);
//        排序
        lqw.orderByAsc(Employee::getUpdateTime);
        employeeService.page(employeePage,lqw);
        return R.success(employeePage);
    }

    /***
     * 添加用户
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){

//        设置初始密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        写入更新创建时间和更新时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
//        获取当前Session的id 强制转换为长整型 雪花函数必须使用Long类型
        Long id =(Long) request.getSession().getAttribute("employee");
        //        写入更新创建人和更新人
        employee.setCreateUser(id);
        employee.setUpdateUser(id);

        employeeService.save(employee);
        return R.success("添加成功");
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if(employee!=null){
            return R.success(employee);
        }
        return R.error("失败");
    }


    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee){
            Long id = (Long) request.getSession().getAttribute("employee");
            employee.setUpdateTime(LocalDateTime.now());
            employee.setCreateUser(id);
            employeeService.updateById(employee);
            return R.success("修改成功");
    }


}
